package io.summer.basis.core;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;

/**
 * @author Moonlight
 */
public abstract class MyApplicationContext implements ApplicationContextAware {
  protected ApplicationContext applicationContext;
  public final static String PROFILE_DEV = "dev";
  public final static String PROFILE_TEST = "test";
  public final static String PROFILE_PROD = "prod";
  public final static String PROFILE_UAT = "uat";

  @Override
  public void setApplicationContext(ApplicationContext context) throws BeansException {
    this.applicationContext = context;
  }

  protected boolean isProfiles(String... profileName) {
    return getEnvironment().acceptsProfiles(Profiles.of(profileName));
  }

  protected Environment getEnvironment() {
    return applicationContext.getEnvironment();
  }

  protected boolean isDev() {
    return isProfiles(PROFILE_DEV);
  }

  protected boolean isProd() {
    return isProfiles(PROFILE_PROD);
  }

  protected boolean isTest() {
    return isProfiles(PROFILE_TEST);
  }

  /**
   * is User Acceptance Test(uat) env?
   *
   * @return
   */
  protected boolean isUat() {
    return isProfiles(PROFILE_UAT);
  }
}
